﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PlugNT.Cms.Model;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_FriendLink
{
    public partial class Add : AdminPage
    {
        private FriendLink curFriendLink = new FriendLink();
        private FriendLinkClass curFriendLinkClass = new FriendLinkClass();

        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            if (!IsPostBack)
            {
                try
                {
                    DdlMenu_bind();
                }
                catch (Exception ex) { Response.Redirect("../Message.aspx?message=绑定异常！" + ex.Message + "&z=no"); }
            }
        }

        #region 类别数据绑定
        void DdlMenu_bind()
        {
            DataTable dt = curFriendLinkClass.GetList();
            this.DdlMenu.Items.Clear();
            this.DdlMenu.Items.Add(new ListItem("请选择所属分类", ""));
            foreach (DataRow dr in dt.Rows)
            {
                string clsno = dr["clsno"].ToString();
                string clsname = dr["clsname"].ToString();
                ListItem li = new ListItem(clsname, clsno);
                DdlMenu.Items.Add(li);
            }
            //DdlMenu.Attributes.Add("onchange", "SwapText();");
        }
        #endregion

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string classno = this.DdlMenu.SelectedValue.Trim();
            if (classno != "")
            {
                FriendLinkInfo model = new FriendLinkInfo();

                model.clsno = classno;
                model.targeturl = txtTargetUrl.Text.Trim();
                model.description = txtDescription.Text.Trim();
                model.text = txtContent.Text.Trim();
                model.type = int.Parse(rdolstTarget1.SelectedItem.Value);
                string images_path = "";
                if (fu_images.PostedFile.ContentLength > 1)
                {
                    ChangeHope.Common.UploadFile uf = new ChangeHope.Common.UploadFile();
                    uf.ExtensionLim = ".gif,.jpg,.jpeg,.dmp";
                    uf.FileLengthLim = 1024;
                    uf.PostedFile = this.fu_images;
                    uf.SavePath = "/UploadFiles/FriendLink";
                    string errorInfo = "";

                    if (uf.Upload())
                    {
                        if (uf.HaveLoad)
                        {

                            images_path = uf.FilePath;
                        }
                        else
                        {

                            errorInfo = uf.Message;
                        }
                    }
                    else
                    {
                        errorInfo = uf.Message;
                    }
                    if (images_path == "")
                    {

                        return;
                    }
                }
                model.image = images_path;

                if (curFriendLink.Add(model))
                {
                    Response.Redirect("../Message.aspx?message=添加链接成功！&z=yes");
                }
                else
                {
                    Response.Redirect("../Message.aspx?message=添加链接失败！&z=no");
                }
            }
            else
            {
                Response.Redirect("../Message.aspx?message=添加链接失败，请选择要添加链接的页面！&z=no");
            }
        }
    }
}
